package com.atguigu.guli.service.edu.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.CourseDescription;
import com.atguigu.guli.service.edu.entity.form.CourseInfoForm;
import com.atguigu.guli.service.edu.entity.query.CourseQuery;
import com.atguigu.guli.service.edu.entity.vo.CourseVo;
import com.atguigu.guli.service.edu.mapper.CourseDescriptionMapper;
import com.atguigu.guli.service.edu.mapper.CourseMapper;
import com.atguigu.guli.service.edu.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-10-26
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    CourseDescriptionMapper courseDescriptionMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String saveCourseInfo(CourseInfoForm courseInfoForm) {

        Course course = new Course();
        BeanUtils.copyProperties(courseInfoForm,course);
        baseMapper.insert(course);
        String description = courseInfoForm.getDescription();
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(description);
        courseDescription.setId(course.getId());
        courseDescriptionMapper.insert(courseDescription);

        return course.getId();
    }

    @Override
    public CourseInfoForm getCourseInfoById(String id) {
        Course course = baseMapper.selectById(id);
        if(course==null){
            return null;
        }
        CourseDescription courseDescription = courseDescriptionMapper.selectById(id);

        CourseInfoForm courseInfoForm = new CourseInfoForm();
        BeanUtils.copyProperties(course,courseInfoForm);
        if(courseDescription!=null){
            courseInfoForm.setDescription(courseDescription.getDescription());
        }else{
            courseInfoForm.setDescription("");
        }
        return courseInfoForm;
    }

    @Override
    public void updateCourseInfo(CourseInfoForm courseInfoForm) {
        //Course
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoForm,course);
        baseMapper.updateById(course);
        //CourseDescription
        String description = courseInfoForm.getDescription();
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(description);
        courseDescription.setId(course.getId());
        int i = courseDescriptionMapper.updateById(courseDescription);
        if (i == 0) {
            courseDescriptionMapper.insert(courseDescription);
        }
    }

    @Override
    public IPage<CourseVo> selectPage(Integer pageNum, Integer pageSize, CourseQuery courseQuery) {

        Page<CourseVo> page = new Page(pageNum,pageSize);

        QueryWrapper<CourseVo> queryWrapper = new QueryWrapper<>();
        String title = courseQuery.getTitle();
        String subjectParentId = courseQuery.getSubjectParentId();
        String subjectId = courseQuery.getSubjectId();
        String teacherId = courseQuery.getTeacherId();

        if(!StringUtils.isEmpty(title)){
            queryWrapper.like("c.title",title);
        }
        if(!StringUtils.isEmpty(subjectParentId)){
            queryWrapper.eq("c.subject_parent_id",subjectParentId);
        }
        if(!StringUtils.isEmpty(subjectId)){
            queryWrapper.eq("c.subject_id",subjectId);
        }
        if(!StringUtils.isEmpty(teacherId)){
            queryWrapper.eq("c.teacher_id",teacherId);
        }
        List<CourseVo> records = baseMapper.selectPageByCourseQuery(page,queryWrapper);
        page.setRecords(records);
        return page;
    }
}
